package com.gpframework.module.controller.manufacture;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.module.manufacture.domain.GpProductionDetails;
import com.gpframework.module.manufacture.domain.req.GpProductionDetailsAdd;
import com.gpframework.module.manufacture.domain.req.GpProductionDetailsReq;
import com.gpframework.module.manufacture.domain.req.GpProductionDetailsUpdate;
import com.gpframework.module.manufacture.domain.vo.GpProductionDetailsVo;
import com.gpframework.module.manufacture.service.IGpProductionDetailsService;
import com.gpframework.module.manufacture.service.IGpProductionSchedulingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GpProductionDetailsController
 * @Author gpframework
 * @Date 2023-12-11
 * @Version 1.0.0
 * @Description 做货明细Controller 接口类
 */
@Validated
@Api(tags = "做货明细")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/manufacture/productionDetails" )
public class GpProductionDetailsController extends BaseController {

    private final IGpProductionDetailsService iGpProductionDetailsService;
    private final IGpProductionSchedulingService iGpProductionSchedulingService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-12-11
  * @Param gpProductionDetails
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @GetMapping("/list")
    public TableDataInfo<GpProductionDetailsVo> list(GpProductionDetailsReq gpProductionDetailsReq)
    {
        startPage();
        QueryWrapper<GpProductionDetails> lqw = new QueryWrapper<GpProductionDetails>();
        gpProductionDetailsReq.generatorQuery(lqw,true);
        List<GpProductionDetailsVo> list = iGpProductionDetailsService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpProductionDetails
     * @Return AjaxResult
     */
    @ApiOperation("详情")
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpProductionDetailsVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpProductionDetailsService.getVoById(id));
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpProductionDetails
     * @Return AjaxResult
     */
    @ApiOperation("新增")
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpProductionDetailsAdd gpProductionDetailsAdd) {
        GpProductionDetails gpProductionDetails = Convert.convert(new TypeReference<GpProductionDetails>() {}, gpProductionDetailsAdd);
        return toAjax(iGpProductionDetailsService.save(gpProductionDetails) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpProductionDetails
     * @Return AjaxResult
     */
    @ApiOperation("修改")
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpProductionDetailsUpdate gpProductionDetailsUpdate) {
        return toAjax(iGpProductionDetailsService.editOne(gpProductionDetailsUpdate) ? 1 : 0);
    }

    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpProductionDetails
     * @Return AjaxResult
     */
    @ApiOperation("删除")
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpProductionDetailsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 生成生产排单表
     * @Author gpframework
     * @Date 2023-12-12
     * @Param gpProductionScheduling
     * @Return TableDataInfo
     */
    @ApiOperation("生成生产排单表")
    @Log(title = "生成生产排单表" , businessType = BusinessType.INSERT)
    @PostMapping("/createProductionScheduling")
    public AjaxResult createProductionScheduling(@Valid @RequestBody GpProductionDetailsUpdate gpProductionDetailsUpdate) {
        if (gpProductionDetailsUpdate == null || gpProductionDetailsUpdate.getId() == null) {
            return AjaxResult.parameterError();
        }
        return toAjax(iGpProductionSchedulingService.createProductionScheduling(gpProductionDetailsUpdate, gpProductionDetailsUpdate.getCreateBy()) ? 1 : 0);
    }
}
